package com.blog.util;

import java.util.LinkedList;
import java.util.List;

import com.blog.constant.Constants;

public class PageUtil {
	
	public static int getTotalPage(int totleRecord,int pageSize) {
		int total = 0;
		if (totleRecord % pageSize == 0) {
			total = totleRecord / pageSize;
		}else {
			total = totleRecord / pageSize + 1;
		}
		return total;	
	}
	
	public static int getStartIndex(int pageSize, int pageNo) {
		int startIndex = 0;
		startIndex = pageSize * (pageNo - 1);
		return startIndex;
	}
	
	public static List<Integer> getPageList(int pageTotal, Integer pageNo) {
		int offset = pageNo % Constants.SHOW_PAGE_NUM;
        int offNum = pageTotal - pageNo;		
		List<Integer> showPage = new LinkedList<>();
		if (offset == 0) {
			for (int i = 0; i < Constants.SHOW_PAGE_NUM; i++) {
				showPage.add(pageNo - Constants.SHOW_PAGE_NUM + 1 + i);
			}
			return showPage;
		}
		if (offset + offNum >= Constants.SHOW_PAGE_NUM) {
			for (int i = 0; i < Constants.SHOW_PAGE_NUM; i++) {
				showPage.add(pageNo - offset + 1 + i);
			}
			return showPage;
		} else {
			for (int i = 0; i < offset + offNum; i++) {
				showPage.add(pageNo - offset + 1 + i);
			}
			return showPage;
		}		
	}
	
}
